<?php
namespace App\Controller\App;

use App\Query\ConfigQuery;
use App\Query\UserQuery;
use App\Repository\UserRepository;
use App\Request\UserLoginRequest;
use App\Request\UserRequest;

use App\Facade\Sms;
use App\Service\Code\CodeService;

use Hyperf\Filesystem\FilesystemFactory;

class IndexController extends Controller
{
    // 配置
    public function config()
    {
        return ConfigQuery::get();
    }
    
    // 搜索用户
    public function search()
    {
        $user = UserQuery::query(
            UserQuery::model(), 
            $this->inputs('username')
        )->first();
        
        return $user ?? $this->error('user');
    }
    
    // ID搜索用户
    public function searchId($id)
    {
        $user = UserQuery::model()->find($id);
        
        return $user ?? $this->error('user');
    }
    
    // 发送验证码
    public function code()
    {
        // 短信内容
        $content = ConfigQuery::get()['sms_' . $this->input('type')];
        
        // 手机号
        $phone = $this->input('phone');
        
        // 生成验证码
        if(!$code = (new CodeService($phone))->make())
        {
            $this->error('code_time');
        }
        
        // 发送验证码
        if(Sms::send($phone, str_replace('{code}', $code, $content)))
        {
            $this->message('ok');
        }
        
        $this->error('error');
    }
    
    // 登陆
    public function login(UserLoginRequest $request)
    {
        // 开始登陆
        if($token = UserRepository::make()->login($request->validated()))
        {
            return $token;
        }
        
        $this->error('login');
    }
    
    // 注册
    public function register(UserRequest $request)
    {
        // 验证验证码
        if(!(new CodeService($this->input('phone')))->check($this->input('code')))
        {
            $this->error('code');
        }

        if(UserRepository::make()->create($request->validated()))
        {
            $this->message('create');
        }
        
        $this->error('create');
    }

    // 找回密码
    public function password()
    {
        // 搜索用户
        if($user = UserRepository::find($this->input('id')))
        {
            // 验证验证码
            if(!(new CodeService($user->phone))->check($this->input('code')))
            {
                $this->error('code');
            }
            
            // 重置密码
            if($user->update($this->inputs('password')))
            {
                $this->message('ok');
            }
        }
        
        $this->error('error');
    }
    
    // 上传图片
    public function upload(FilesystemFactory $factory)
    {
        if(!$this->request->hasFile('file'))
        {
            $this->error('upload');
        }
        
        $file = $this->request->file('file');

        // 检查文件
        if(!in_array($file->getExtension(), ['jpeg', 'jpg', 'gif', 'png']))
        {
            $this->error('upload_ext');
        }
        
        // 真实文件
        $stream = fopen($file->getRealPath(), 'r+');
        
        // 文件内容
        $content = fread($stream, filesize($file->getRealPath()));
        
        // 关闭文件
        fclose($stream);
        
        // 路径
        $url = '/upload/app/' . date('Y-m/d') . '/' . md5($content) . '.' . $file->getExtension();
        
        // 保存文件
        $ftp = $factory->get('ftp');
        
        // 如果文件存在将不上传
        if(!$ftp->has($url))
        {
            // 上传
            $ftp->write($url, $content);
        }
        
        return ['url' => $url];
    }
}
